package com.example.demo.util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Component;

@Component
public class RedisHashUtil {

	@Autowired
	private HashOperations<String, String, Object> hashOperations;
	
	public void hset(String key, String field, Object value) {
		hashOperations.put(key, field, value);
	}
	
	public Object hget(String key, String field) {
		return hashOperations.get(key, field);
	}
	
	public boolean hexists(String key, String field) {
		return hashOperations.hasKey(key, field);
	}
	
	public long hlen(String key) {
		return hashOperations.size(key);
	}
	
	public Set<String> hkeys(String key) {
		return hashOperations.keys(key);
	}
	
	public List<Object> hvals(String key){
		return hashOperations.values(key);
	}
	
	public long hdel(String key, String ...fields) {
		return hashOperations.delete(key, fields);
	}
	
	public boolean hsetnx(String key, String field, String value) {
		return hashOperations.putIfAbsent(key, field, value);
	}
	
	public long hincrby(String key, String field, long increment) {
		return hashOperations.increment(key, field, increment);
	}
	
	public Map<String, Object> hgetall(String key) {
		return hashOperations.entries(key);
	}
	
	public void hmset(String key, Map<String, Object> map) {
		hashOperations.putAll(key, map);
	}
	
	public List<Object> hmget(String key, String ...fields) {
		return hashOperations.multiGet(key, Arrays.asList(fields));
	}
}
